/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import javax.swing.*;
import java.awt.event.*;

import blueprint4j.Constants;
import blueprint4j.utils.*;

import java.awt.*;

public class MediaToolbar extends JPanel implements ActionListener {
	public static final int DO_TOBEGIN = 1;
	public static final int DO_REWIND = 2;
	public static final int DO_PLAY = 4;
	public static final int DO_PAUSE = 8;
	public static final int DO_STOP = 16;
	public static final int DO_FASTFORWARD = 32;
	public static final int DO_TOEND = 64;

	private int fields = 0;
	private MediaToolbarListener listener = null;

	private AnimatingButton play =null,stop =null,pause =null,rewind =null,fastforward = null,
	                              tobegin =null,toend =null;

	/**
	 * Set the Listene, The layout manager for the 6 buttons, and the fields
	 */
	public MediaToolbar(MediaToolbarListener listener,int fields) {
		this(listener,new GridLayout(2,3),fields);
	}

	public MediaToolbar(MediaToolbarListener listener,LayoutManager layout,int fields) {
		try {
			this.listener =listener;
			this.fields = fields;

			play =new  AnimatingButton(null, new String[]{Constants.ICON_BASE+"actions/player_play.png"}, 1);
			stop =new AnimatingButton(null, new String[]{Constants.ICON_BASE+"actions/player_stop.png"}, 1);
			pause =new AnimatingButton(null, new String[]{Constants.ICON_BASE+"actions/player_pause.png"}, 1);
			rewind =new AnimatingButton(null, new String[]{Constants.ICON_BASE+"actions/player_rew.png"}, 1);
			tobegin =new AnimatingButton(null, new String[]{Constants.ICON_BASE+"actions/player_start.png"}, 1);
			toend =new AnimatingButton(null, new String[]{Constants.ICON_BASE+"actions/player_end.png"}, 1);
			fastforward =new AnimatingButton(null, new String[]{Constants.ICON_BASE+"actions/player_fwd.png"}, 1);

			setLayout(layout);
			if ((fields & 0x01)>>0 == 1) {
				tobegin.addActionListener(this);
				add(tobegin);
			}
			if ((fields & 0x02)>>1== 1) {
				rewind.addActionListener(this);
				add(rewind);
			}
			if ((fields & 0x04)>>2== 1) {
				play.addActionListener(this);
				add(play);
			}
			if ((fields & 0x08)>>3== 1) {
				pause.addActionListener(this);
				add(pause);
			}
			if ((fields & 0x10)>>4== 1) {
				stop.addActionListener(this);
				add(stop);
			}
			if ((fields & 0x20)>>5== 1) {
				fastforward.addActionListener(this);
				add(fastforward);
			}
			if ((fields & 0x40)>>6== 1) {
				toend.addActionListener(this);
				add(toend);
			}
		} catch (Exception exception) {
			Log.critical.out(exception);
		}
	}

	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == tobegin) {
			listener.tobegin();
		}
		if (event.getSource() == rewind) {
			listener.rewind();
		}
		if (event.getSource() == play) {
			listener.play();
		}
		if (event.getSource() == pause) {
			listener.pause();
		}
		if (event.getSource() == stop) {
			Log.trace.out("Asked to stop");
			listener.stop();
		}
		if (event.getSource() == fastforward) {
			listener.fastforward();
		}
		if (event.getSource() == toend) {
			listener.toend();
		}
	}

	public interface MediaToolbarListener {
		public void play();
		public void rewind();
		public void fastforward();
		public void stop();
		public void pause();
		public void toend();
		public void tobegin();
	}
}
